/// @tags: SAM LCT
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

namespace BlueQuantum {

typedef long long LL;
typedef pair<int, int> pii;

int const N = 2e5 + 5, INF = 0x3f3f3f3f;

inline void min(int& x, int y) {
  if (x > y) x = y;
}

namespace SAM {

int tot = 1, lst = 1;
int ch[N][26], link[N], len[N];

inline int extend(int x) {
  int pos(lst), cur, q, nq;
  len[cur = lst = ++tot] = len[pos] + 1;
  while (pos && !ch[pos][x]) ch[pos][x] = cur, pos = link[pos];
  if (!pos) return link[cur] = 1, lst;
  if (len[q = ch[pos][x]] == len[pos] + 1) return link[cur] = q, lst;
  memcpy(ch[nq = ++tot], ch[q], sizeof(ch[q]));
  len[nq] = len[pos] + 1, link[nq] = link[q], link[q] = link[cur] = nq;
  while (pos && ch[pos][x] == q) ch[pos][x] = nq, pos = link[pos];
  return lst;
}

}  // namespace SAM

namespace BIT {

LL c[N], d[N];

inline int lowbit(int x) { return x & -x; }

inline void add(int x, int y) {
  for (LL w = x * y; x < N; x += lowbit(x)) c[x] += y, d[x] += w;
}

inline void add(int l, int r, int x) { add(l, x), add(r + 1, -x); }

inline LL query(int x) {
  LL out = 0, tmp = 0;
  for (int p = x; p; p -= lowbit(p)) out += c[p], tmp += d[p];
  return out * (x + 1) - tmp;
}

inline LL query(int l, int r) { return query(r) - query(l - 1); }

}  // namespace BIT

namespace LCT {

int fa[N], ch[N][2], last[N], tag[N], len[N], val[N];

inline bool iden(int x) { return x == ch[fa[x]][1]; }

inline bool isnrt(int x) { return x == ch[fa[x]][iden(x)]; }

inline void pushup(int x) {
  val[x] = len[x];
  min(val[x], val[ch[x][0]]);
  min(val[x], val[ch[x][1]]);
}

inline void rotate(int x) {
  int f = fa[x], gf = fa[f], xtofa = iden(x), son = ch[x][xtofa ^ 1];
  if (isnrt(f)) ch[gf][iden(f)] = x;
  if (son) fa[son] = f;
  fa[f] = x;
  fa[x] = gf;
  ch[x][xtofa ^ 1] = f;
  ch[f][xtofa] = son;
  pushup(f), pushup(x);
}

inline void add(int x, int w) { last[x] = tag[x] = w; }

inline void pushdown(int x) {
  if (tag[x]) add(ch[x][0], tag[x]), add(ch[x][1], tag[x]), tag[x] = 0;
}

void pushall(int x) {
  if (isnrt(x)) pushall(fa[x]);
  pushdown(x);
}

inline void splay(int x) {
  pushall(x);
  for (; isnrt(x); rotate(x))
    if (isnrt(fa[x])) rotate(iden(x) == iden(fa[x]) ? fa[x] : x);
}

inline void access(int x, int pos) {
  for (int p = x, f = 0; p; p = fa[f = p]) {
    splay(p), ch[p][1] = f, pushup(p);
    if (last[p]) BIT::add(last[p] - SAM::len[p] + 1, last[p] - val[p] + 1, -1);
  }
  splay(x), add(x, pos), BIT::add(pos - SAM::len[x] + 1, pos, 1);
}

inline void init() {
  val[0] = INF;
  for (int i = 1; i <= SAM::tot; ++i) {
    val[i] = len[i] = SAM::len[fa[i] = SAM::link[i]] + 1;
  }
}

}  // namespace LCT

char s[N];
int n, m, pos[N];
LL ans[N];
vector<pii> q[N];

inline int main() {
  cin >> (s + 1) >> m;
  n = strlen(s + 1);
  for (int i = 1, l, r; i <= m; ++i) cin >> l >> r, q[r].push_back(pii(i, l));
  for (int i = 1; i <= n; ++i) pos[i] = SAM::extend(s[i] - 'a');
  LCT::init();
  for (int i = 1; i <= n; ++i) {
    LCT::access(pos[i], i);
    for (vector<pii>::iterator j = q[i].begin(), jlim = q[i].end(); j != jlim; ++j)
      ans[j->first] = BIT::query(j->second, i);
  }
  for (int i = 1; i <= m; ++i) cout << ans[i] << '\n';
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P6292 区间本质不同子串个数.in", "r", stdin);
  freopen("P6292 区间本质不同子串个数.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
